import request from '@/axios'
import { check, checkResultPage, Page, ResultPage } from "./baseModel"

export interface User {
    id: string
    username: string
    phone: string
    image: string
    orderCount: number
    totalConsumption: number
    createTime: string
    lastLoginTime: string
    status: boolean
}

export interface UserSearch {
    username: string | undefined
    phone: string | undefined
    status: boolean | undefined
}

export function getInitUserSearch(): UserSearch {
    return {
        username: undefined,
        phone: undefined,
        status: undefined
    }
}

export class UserApi {

    static baseUrl = "/user"

    // 分页获取数据
    static async pagingSearch(search: UserSearch, page: Page): Promise<ResultPage<User>> {
        try {
            const response = await request.get(
                `${this.baseUrl}/search`,
                { params: { ...search, ...page }, data: { loading: '加载数据' } }
            )
            return checkResultPage(response) as ResultPage<User>
        }
        catch (error: any) { throw error }
    }

    // 修改用户状态
    static async setStatus(id: string, status: boolean) {
        try {
            const response = await request.put(
                `${this.baseUrl}/` + id + "?status=" + status,
                { data: { loading: '加载数据' } }
            )
            return check(response)
        }
        catch (error: any) { throw error }
    }

}